package com.chen.demo.service.imp;

import java.util.Date;
/**
 * service接口的实现
 */
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chen.demo.dao.AreaDao;
import com.chen.demo.domain.Area;
import com.chen.demo.service.dao.AreaServiceDao;

@Service
public class AreaServiceImpl implements AreaServiceDao {

	@Autowired
	private AreaDao dao;

	private static final Logger logger = LogManager.getLogger(AreaServiceImpl.class);

	/**
	 * 1查询区域的列表
	 */
	@Override
	public List<Area> getAreaList() {
		return dao.queryArea();
	}

	/**
	 * 2根据id查询单个区域
	 */
	@Override
	public Area getAreaById(int areaId) {
		return dao.queryAreaById(areaId);
	}

	/**
	 * 3添加区域信息 需要事务的控制
	 */
	@Transactional
	@Override
	public boolean addArea(Area area) {
		String name = area.getAreaName();
		if (name != null && !"".equals(name)) {
			area.setCreateTime(new Date());
			area.setLastEditTime(new Date());
			try {
				int no = dao.insertArea(area);
				if (no > 0) {
					logger.info("success");
					return true;
				} else {
					logger.info("default");
				}
			} catch (Exception e) {
				logger.info("插入失败" + e.toString());
			}
		} else {
			logger.info("区域信息不能为空");
		}
		return false;
	}

	/**
	 * 4 更新区域信息
	 */
	@Override
	public boolean modifyArea(Area area) {
		Integer id = area.getAreaId();
		if (id != null && id > 0) {
			area.setLastEditTime(new Date());
			try {
				int no = dao.updateArea(area);

				if (no > 0) {
					logger.info("success");
					return true;
				} else {
					logger.info("default");
				}
			} catch (Exception e) {
				logger.info("插入失败" + e.toString());
			}
		} else {
			logger.info("区域信息不能为空");
		}
		return false;
	}

	/**
	 * 5删除区域信息
	 */
	@Override
	public boolean deleteArea(int areaId) {
		if (areaId > 0) {
			try {
				int no = dao.deleteArea(areaId);
				if (no > 0) {
					logger.info("success");
					return true;
				} else {
					logger.info("default");
				}
			} catch (Exception e) {
				logger.info("删除失败" + e.toString());
			}
		} else {
			logger.info("区域id不能为空");
		}
		return false;
	}
}
